package com.crm.services;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crm.model.mdao.PowerMapper;
import com.crm.model.mpojo.Power;
import com.crm.util.MapmenusUtil;
import com.github.pagehelper.PageHelper;

@Service
public class PowerService {
	
	@Autowired
	PowerMapper powerMapper;
	
	/**
	 * 分页查询(菜单列表)
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<Map<String, String>> selectByPager(Integer pageNum, Integer pageSize,Integer statusid,String search) {
		// 使用分页插件
		System.out.println("pageNum:" + pageNum + ",pageSize:" + pageSize);
		// 配置分页信息。这句话后面的第一个查询将调用分页。使用分页，对应的Service方法最好只写一个查询方法
		PageHelper.startPage(pageNum, pageSize);
		// 获取
		List<Map<String, String>> list = powerMapper.selectMenuAllTable(statusid,search);
		System.out.println("大小" + list.size());
		return list;
	}
	
	/**
	 * 保存菜单
	 * @param power
	 * @return
	 */
	public int save(Power power) {
		return powerMapper.insertSelective(power);
	}
	
	/**
	 * 根据用户获取权限菜单
	 * @return 
	 * 
	 */
	public List<MapmenusUtil> menusList(Integer uid) {
		List<MapmenusUtil> list = new ArrayList<>();
		//获取所有菜单列表
		List<Map<String, String>> selectMenuAll = powerMapper.selectMenusUid(uid);
		for (Map<String, String> map : selectMenuAll) {
			MapmenusUtil menus = new MapmenusUtil();
			List<Map<String,String>> mapList=new  ArrayList<>();
			//查找顶级菜单
			if (String.valueOf(map.get("parentId")).equals("0")) {
				menus.setMap(map);
				//循环遍历查找二级菜单
				for (Map<String, String> map1 : selectMenuAll) {
					if (String.valueOf(map.get("pId")).equals(String.valueOf(map1.get("parentId")))) {
						mapList.add(map1);
					}
				}
				menus.setMapList(mapList);
				list.add(menus);
			}
		}
		return list;
	}
	
	/**
	 * 获取所有权限菜单
	 * @return 
	 * 
	 */
	public List<MapmenusUtil> menusListAll() {
		List<MapmenusUtil> list = new ArrayList<>();
		//获取所有菜单列表
		List<Map<String, String>> selectMenuAll = powerMapper.selectMenuAll();
		for (Map<String, String> map : selectMenuAll) {
			MapmenusUtil menus = new MapmenusUtil();
			List<Map<String,String>> mapList=new  ArrayList<>();
			//查找顶级菜单
			if (String.valueOf(map.get("parentId")).equals("0")) {
				menus.setMap(map);
				//循环遍历查找二级菜单
				for (Map<String, String> map1 : selectMenuAll) {
					if (String.valueOf(map.get("pId")).equals(String.valueOf(map1.get("parentId")))) {
						mapList.add(map1);
					}
				}
				menus.setMapList(mapList);
				list.add(menus);
			}
		}
		return list;
	}
	
}
